/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package client;

import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Farzam
 */
public class ServerComm {
    private static Socket socket;
    private static OutputStream os;

    
    static void register(String user, String pass, String ip) throws IOException {
            String msg_out = "type:command;action:register;param:2;user:"+user+";password:"+pass+";";
            send_data(ip, msg_out);
    }

    void login(String user, String pass, String ip) throws IOException {
            String msg_out = "type:command;action:login;param:2;user:"+user+";password:"+pass+";";
            send_data(ip, msg_out);
    }

    void ping_reply(String ip) throws IOException {
        String msg_out = "type:reply;status:ok";
        send_data(ip, msg_out);
    }

    void get_player_list(String ip) throws IOException {
        String msg_out = "type:command;action:send_client_list;";
        send_data(ip, msg_out);
    }

    void create_game(String _myUser, int player_total, String ip) throws IOException {
        String msg_out = "type:command;action:create_game;owner:"+_myUser+";max_player:"+player_total+";";
        send_data(ip, msg_out);
    }

    void get_game_list(String ip) throws IOException {
        String msg_out = "type:command;action:send_game_list;";
        send_data(ip, msg_out);
    }

    void cancel_game(String _myUser, String ip) throws IOException {
        String msg_out = "type:command;action:cancel_game;game_owner:"+_myUser+";";
        send_data(ip, msg_out);
    }

    void leave_game(String game_owner, String _myUser, String ip) throws IOException {
        String msg_out = "type:command;action:leave_game;user:"+_myUser+";game_owner:"+game_owner+";";
        send_data(ip, msg_out);
    }

    void join_game(String game_owner, String _myUser, String ip, int team) throws IOException {
        String msg_out = "type:command;action:join_game;me:"+_myUser+";owner:"+game_owner+";team:"+team+";";
        send_data(ip, msg_out);
    }
 //   public void gamemsg(String msg, String ip) throws IOException {
 //       String msg_out = "type:gamedata;"+msg;
 //       send_data(ip, msg_out);
 //   }
 //   public void gamestart(String msg, String ip) throws IOException{
 //       String msg_out= "type:gamestart";
 //       send_data(msg_out,ip);
 //   }

    private static void send_data(String ip, String msg_out) throws IOException {
        System.out.println("Sending data: "+msg_out+";");
            try {
                socket = new Socket(ip, 5000);
            } catch (UnknownHostException ex) {
                Logger.getLogger(ServerComm.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Don't know about host: " + ip);
            } catch (IOException ex) {
                Logger.getLogger(ServerComm.class.getName()).log(Level.SEVERE, null, ex);
                System.err.println("Couldn't get I/O for the connection to: "+ip+".");
                throw ex;
            }
            Writer out = null;
            try {
                out = new OutputStreamWriter(socket.getOutputStream());
                
            } catch (IOException ex) {
                Logger.getLogger(ServerComm.class.getName()).log(Level.SEVERE, null, ex);
                throw ex;
            }
            out = new BufferedWriter(out);
            try {
                out.write(msg_out);
            } catch (IOException ex) {
                Logger.getLogger(ServerComm.class.getName()).log(Level.SEVERE, null, ex);
                throw ex;
            }
            out.close();
            socket.close();
    }
















}
        



